#!/usr/bin/python

#Copyright 2012 Elias Farhan
#
#This file is part of Squirrel.
#
#Squirrel is free software: you can redistribute it and/or modify it under the terms of the GNU General 
#Public License as published by the Free Software Foundation, either version 3 of the License, or (at your 
#option) any later version.
#
#Squirrel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#See the GNU General Public License for more details.
import pygame
import os
import animation
from box import Box

path = os.path.abspath(".")

def load_image(filename, size):
	image = pygame.image.load(path+"/"+filename)
	image = pygame.transform.scale(image, size)
	image = image.convert_alpha()
	return image
class GameObject(pygame.sprite.Sprite):
	
	def __init__(self, pos, size, filename):
		pygame.sprite.Sprite.__init__(self)
		self.filename = filename
		self.pos = pos #upleft position
		self.size = size
		self.boxes = []
		self.image = None
	def setImage(self, image):
		self.image = image
	def addBoxes(self, box):
		self.boxes.append(box)
	def getBoxes(self):
		return self.boxes
	def resize(self, newsize):
		self.size = newsize
		if(self.image != None):
			self.image = pygame.transform.scale(self.image, self.size)

class Character(GameObject):
	def __init__(self, pos, size, filename):
		GameObject.__init__(self, pos, size, filename)
		#Defined for player movement
		self.movement = animation.Moving()
		#self.initState()

		self.img = []
		self.count = 0
		
		self.nearbox = []
	def initState(self):
		#done in the xmlreader
		self.movement.addState("WALK", 3.5, (4, 4, 4, 4), 0)
		self.movement.addBox(Box((105, 105), (157, 147)), 0)
		self.movement.addState("RUN", 10, (4, 4, 4, 4), 16)
		self.movement.addBox(Box((105, 105), (157, 147)), 1)
	def setState(self, new, isUp):
		#run or walk
		if(isUp):
			self.movement.setState(0)
		else:
			self.movement.setState(1)
	def getBoxes(self):
		return self.movement.getBoxes()
	def updateSpeed(self):
		(self.speedh, self.speedv) = self.movement.getSpeed()
	def update(self):
		#TODO check if the nearbox are colliding after the move
		self.updateSpeed()
		self.boxes = self.getBoxes()
		if (self.count == 0):
			self.nextImg()
		self.count = (self.count+1)%4
		pos_tmp = (self.pos[0]+self.speedh, self.pos[1]+self.speedv)
		chock = False
		for playerbox in self.globaltmpBox(pos_tmp):
			for near in self.nearbox:
				#print "player", self.pos, playerbox.origin, playerbox.end, "near", near.origin, near.end
				if(near.BoxIsIn(playerbox)):
					chock = True
		if(not chock):
			self.pos = pos_tmp
		self.rect = self.image.get_rect()
	def globaltmpBox(self, pos):
		boxes = []
		for box in self.boxes:
			#print "globaltmpbox", pos, box.origin, box.end
			boxes.append(Box((pos[0]+box.origin[0], pos[1]+box.origin[1]), 
							(pos[0]+box.end[0], pos[1]+box.end[1])))
			#print "endglobal", boxes[len(boxes)-1].origin, boxes[len(boxes)-1].end
		return boxes
	def globalBox(self):
		return self.globaltmpBox(self.pos)
	#def setImage(self, img):
	#	self.img = img
	#	self.nextImg()
	def nextImg(self):
		self.currentImg = self.movement.next()
		self.image = self.img[self.currentImg]
	def setCurrent(self, current, isUp):
		self.movement.setCurrent(current, isUp)
	def setNearBox(self, boxes):
		self.nearbox = boxes
	def addState(self, name, speed, frames, index):
		self.movement.addState(name, speed, frames, index)
	def addBox(self, box, state):
		self.movement.addBox(box, state)
